package listadapter;

import android.content.Context;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import co.yeye.test.Constance;
import co.yeye.test.MainActivity;
import co.yeye.test.R;
import models.DiscoveryObject;
import models.UserObject;

/**
 * Created by yeye on 15/2/18.
 */
public class DiscoveryItemAdapter extends BaseAdapter {

    ImageLoader imageLoader;
    MainActivity mainActivity;
    DisplayImageOptions options;
    ArrayList<DiscoveryObject> objectList = new ArrayList<DiscoveryObject>();
    int count=0;
    private String userCode="-1";

    onClickPicListener picListener;
    onBottomBtnListener btnListener;

    public DiscoveryItemAdapter(MainActivity mainActivity , ImageLoader imageLoader , DisplayImageOptions options){
        this.mainActivity=mainActivity;
        this.imageLoader=imageLoader;
        this.options=options;
        getUserCode();
    }

    /**
     * 获取登录用户的code
     * 切换用户的时候记得调用该方法
     */
    public void getUserCode(){
        UserObject userObject = MainActivity.getInstance().getUser();
        // 确保已经登录
        if(!userObject.getName().equals("-1"))
            userCode=userObject.getUser_code();
        else
            userCode="-1";
        notifyDataSetChanged();
    }

    public void setDiscoveryObjectList(ArrayList<DiscoveryObject> objectList){
        this.objectList=objectList;
        //this.objectList = objectList;
        count=objectList.size();
    }

    public void addDiscoveryObjectList(ArrayList<DiscoveryObject> objectList){
        this.objectList.addAll(objectList);
        //this.objectList = objectList;
        count+=objectList.size();
    }

    private class ViewHolder {
        private ImageView userImage;
        private TextView userName;
        private TextView editDate;
        private TextView content;
        private ImageView img_1,img_2,img_3;
        private ImageView img_4,img_5,img_6;
        private ImageView img_7,img_8,img_9;
        private LinearLayout layout_1,layout_2,layout_3;
        private Button btn_zhan,btn_delete;
        private ImageView divider;
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = mainActivity.getLayoutInflater().inflate(
                    R.layout.discovery_fragment_adapter, viewGroup, false);
            holder = new ViewHolder();
            holder.userImage = (ImageView) view.findViewById(R.id.iv_userimg);
            holder.userName = (TextView) view.findViewById(R.id.iv_username);
            holder.editDate = (TextView) view.findViewById(R.id.iv_date);
            holder.content = (TextView) view.findViewById(R.id.middle_text);
            holder.img_1 = (ImageView) view.findViewById((R.id.iv_img_1));
            holder.img_2 = (ImageView) view.findViewById((R.id.iv_img_2));
            holder.img_3 = (ImageView) view.findViewById((R.id.iv_img_3));
            holder.img_4 = (ImageView) view.findViewById((R.id.iv_img_4));
            holder.img_5 = (ImageView) view.findViewById((R.id.iv_img_5));
            holder.img_6 = (ImageView) view.findViewById((R.id.iv_img_6));
            holder.img_7 = (ImageView) view.findViewById((R.id.iv_img_7));
            holder.img_8 = (ImageView) view.findViewById((R.id.iv_img_8));
            holder.img_9 = (ImageView) view.findViewById((R.id.iv_img_9));
            holder.layout_1 = (LinearLayout) view.findViewById(R.id.image_set_top);
            holder.layout_2 = (LinearLayout) view.findViewById(R.id.image_set_middle);
            holder.layout_3 = (LinearLayout) view.findViewById(R.id.image_set_bottom);
            holder.btn_zhan = (Button) view.findViewById(R.id.iv_btn_zhan);
            holder.btn_delete = (Button) view.findViewById(R.id.iv_btn_delete);
            holder.divider = (ImageView) view.findViewById(R.id.iv_divider);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }

        setImageClickListener(holder,position);
        setBottomClickListener(holder,position);

        hideUselessImages(holder,objectList.get(position).getImg_list().size());

        imageLoader.displayImage(objectList.get(position).getUser_img_url(),holder.userImage,options,null);
        holder.userName.setText(objectList.get(position).getUser_name());
        holder.editDate.setText(objectList.get(position).getDate());
        holder.content.setText(objectList.get(position).getContent());

        // 配置图像读取器
        switch(objectList.get(position).getImg_list().size()){
            case 9 : imageLoader.displayImage(objectList.get(position).getImg_list().get(8),holder.img_9,options, null);
            case 8 : imageLoader.displayImage(objectList.get(position).getImg_list().get(7),holder.img_8,options, null);
            case 7 : imageLoader.displayImage(objectList.get(position).getImg_list().get(6),holder.img_7,options, null);
            case 6 : imageLoader.displayImage(objectList.get(position).getImg_list().get(5),holder.img_6,options, null);
            case 5 : imageLoader.displayImage(objectList.get(position).getImg_list().get(4),holder.img_5,options, null);
            case 4 : imageLoader.displayImage(objectList.get(position).getImg_list().get(3),holder.img_4,options, null);
            case 3 : imageLoader.displayImage(objectList.get(position).getImg_list().get(2),holder.img_3,options, null);
            case 2 : imageLoader.displayImage(objectList.get(position).getImg_list().get(1),holder.img_2,options, null);
            case 1 : imageLoader.displayImage(objectList.get(position).getImg_list().get(0),holder.img_1,options, null);
            default : break;
        }

        // 配置底部删除按钮，只有原作者可以删除自己的消息
        if(objectList.get(position).getCode().equals(userCode)) {
            holder.btn_delete.setVisibility(View.VISIBLE);
            holder.divider.setVisibility(View.VISIBLE);
        }
        else {
            holder.btn_delete.setVisibility(View.GONE);
            holder.divider.setVisibility(View.GONE);
        }

        return view;
    }

    /**
     * 配置九宫格的图片，没有使用的九宫格隐藏
     * 注意隐藏顺序
     */
    private void hideUselessImages(ViewHolder holder , int imageNum){
        // 首先全部显示出来
        holder.img_1.setVisibility(View.VISIBLE);
        holder.img_2.setVisibility(View.VISIBLE);
        holder.img_3.setVisibility(View.VISIBLE);
        holder.img_4.setVisibility(View.VISIBLE);
        holder.img_5.setVisibility(View.VISIBLE);
        holder.img_6.setVisibility(View.VISIBLE);
        holder.img_7.setVisibility(View.VISIBLE);
        holder.img_8.setVisibility(View.VISIBLE);
        holder.img_9.setVisibility(View.VISIBLE);
        holder.layout_1.setVisibility(View.VISIBLE);
        holder.layout_2.setVisibility(View.VISIBLE);
        holder.layout_3.setVisibility(View.VISIBLE);
        // 此处故意不做break
        switch(imageNum){
            case 0 : holder.img_1.setVisibility(View.GONE);holder.layout_1.setVisibility(View.GONE);
            case 1 : holder.img_2.setVisibility(View.GONE);
            case 2 : holder.img_3.setVisibility(View.GONE);
            case 3 : holder.img_4.setVisibility(View.GONE);holder.layout_2.setVisibility(View.GONE);
            case 4 : holder.img_5.setVisibility(View.GONE);
            case 5 : holder.img_6.setVisibility(View.GONE);
            case 6 : holder.img_7.setVisibility(View.GONE);holder.layout_3.setVisibility(View.GONE);
            case 7 : holder.img_8.setVisibility(View.GONE);
            case 8 : holder.img_9.setVisibility(View.GONE);
            case 9 : break;
            default: break;
        }
    }

    private void setImageClickListener(ViewHolder holder, final int position){
        holder.img_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),1);
            }
        });
        holder.img_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),2);
            }
        });
        holder.img_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),3);
            }
        });
        holder.img_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),4);
            }
        });
        holder.img_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),5);
            }
        });
        holder.img_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),6);
            }
        });
        holder.img_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),7);
            }
        });
        holder.img_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),8);
            }
        });
        holder.img_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picListener.picClick(objectList.get(position),9);
            }
        });
    }

    private void setBottomClickListener(ViewHolder holder , final int position){
        holder.btn_zhan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnListener.bottomBtnClick(objectList.get(position),
                        Constance.DISCOVERY_ITEM_ZHAN,position);
            }
        });

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnListener.bottomBtnClick(objectList.get(position),
                        Constance.DISCOVERY_ITEM_DELETE,position);
            }
        });
    }

    /**
     * 配置图片点击监听器
     * @param picListener
     */
    public void setOnClickPicListener(onClickPicListener picListener){
        this.picListener=picListener;
    }

    public interface onClickPicListener{
        /**
         * 点击图片
         * @param picNum 点击的图片编号
         */
        abstract void picClick(DiscoveryObject obj , int picNum);
    }


    /**
     * 配置底部按钮监听器
     */
    public void setOnBottomBtnListener(onBottomBtnListener btnListener){
        this.btnListener=btnListener;
    }

    public interface onBottomBtnListener{
        /**
         * 点击底部按钮事件
         */
        abstract void bottomBtnClick(final DiscoveryObject obj , int button , int position);
    }

}
